include("shared.lua")

ENT.RenderGroup = RENDERGROUP_TRANSLUCENT

function ENT:Initialize()
	self:SetRenderBounds(Vector(-1024, -1024, -1024), Vector(1024, 1024, 1024))
	self:SetSolid(SOLID_NONE)
	self.Seed = math.Rand(0, 10)

	self.Emitter = ParticleEmitter(self:GetPos())
	self.Emitter:SetNearClip(24, 32)
end

local colNext = COLOR_LIMEGREEN
local colSecondNext = COLOR_YELLOW
local colCurrent = color_white
local colWrong = COLOR_RED
local matRing = Material("effects/select_ring")
function ENT:DrawTranslucent()
	local point = self:GetPoint()

	local col = colCurrent
	if MySelf:IsValid() and MySelf:Team() == TEAM_RACING then
		local curpoint = MySelf:Deaths()
		if point == 0 and curpoint == GAMEMODE.MaxCheckpoint or curpoint + 1 == point then
			col = colNext
		elseif curpoint + 2 == point then
			col = colSecondNext
		elseif point ~= curpoint then
			col = colWrong
		end
	end

	local pos = self:GetPos()

	local realtime = RealTime() + self.Seed

	if col == colNext then
		render.SetMaterial(matRing)
		local size = math.sin(realtime * 7.5) * 100 + 256
		render.DrawSprite(pos, size, size, col)
		render.DrawSprite(pos, size * 1.3, size * 1.3, col)
		render.DrawSprite(pos, size * 1.5, size * 1.5, col)
		render.DrawSprite(pos, size * 1.9, size * 1.9, col)

		local velang = EyeAngles()
		velang:RotateAroundAxis(velang:Forward(), math.Rand(0, 360))
		local vel = velang:Up() * 256
		local particle = self.Emitter:Add("effects/spark", pos - vel)
		particle:SetVelocity(vel * 4.5)
		particle:SetDieTime(0.75)
		particle:SetStartAlpha(255)
		particle:SetEndAlpha(64)
		particle:SetStartSize(math.Rand(size * 0.1, size * 0.15))
		particle:SetEndSize(0)
		particle:SetAirResistance(280)
		particle:SetAngles(Angle(velang.roll / -57.296, 0, 0))
		particle:SetColor(math.Rand(130, 160), math.Rand(220, 255), math.Rand(130, 160))

		local ang = EyeAngles()
		ang:RotateAroundAxis(ang:Right(), -ang.pitch)
		ang:RotateAroundAxis(ang:Up(), -90)
		ang:RotateAroundAxis(ang:Forward(), 90 + math.sin(realtime * 5) * 35)

		local r = math.abs(math.sin(realtime * 4) * 255)
		cam.Start3D2D(pos, ang, 1.5 + math.abs(math.cos(realtime * 8)) * 2.5)
			draw.SimpleTextShadow("Checkpoint!", "ScoreboardHead", 0, 0, Color(r, r * 0.2, 255 - r, 255), Color(0,0,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		cam.End3D2D()
	else
		render.SetMaterial(matRing)
		local size = math.sin(realtime * 4) * 32 + 256
		render.DrawSprite(pos, size, size, col)
	end
end
